package com.swind.fast.modules.app.railway.project.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.swind.fast.modules.app.railway.project.constant.ProjectStatusEnum;

import com.swind.fast.common.api.vo.Result;
import com.swind.fast.common.system.query.QueryGenerator;
import com.swind.fast.common.util.oConvertUtils;
import com.swind.fast.modules.app.railway.project.entity.RailwayProject;
import com.swind.fast.modules.app.railway.project.vo.RailwayProjectVo;
import com.swind.fast.modules.app.railway.project.service.IRailwayProjectService;
import com.swind.fast.modules.app.railway.project.vo.RailwayProjectVo;
import com.swind.fast.modules.app.railway.project.vo.RailwayProjectStatisticVo;
import com.swind.fast.modules.app.railway.project.vo.RailwayDataManagementStatisticVo;
import com.swind.fast.modules.app.railway.project.entity.RailwayProjectOperationLog;

import com.swind.fast.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/app/railway/project")
@Api(tags="项目模块")
@Slf4j
public class RailwayProjectController {

    @Autowired
    private IRailwayProjectService railwayProjectService;

    @GetMapping(value = "/list")
    public Result<IPage<RailwayProject>> queryPageList(RailwayProject railwayProject,
                                      @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                      @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                      HttpServletRequest req) {
        railwayProject = railwayProjectService.getUserSpecificProjects(railwayProject);

        QueryWrapper<RailwayProject> queryWrapper = QueryGenerator.initQueryWrapper(railwayProject, req.getParameterMap());
        Page<RailwayProject> page = new Page<RailwayProject>(pageNo, pageSize);
        IPage<RailwayProject> pageList = railwayProjectService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody RailwayProject railwayProject) {
        railwayProjectService.saveMain(railwayProject);
        return Result.OK("添加成功！", railwayProject);
    }

    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody RailwayProject railwayProject) {
        railwayProjectService.updateById(railwayProject);
        return Result.OK("编辑成功!");
    }

    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        // railwayProjectService.removeById(id);
        railwayProjectService.deleteMainById(id);
        return Result.OK("删除成功!");
    }

    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        RailwayProjectVo railwayProject = railwayProjectService.getProjectById(id);
        return Result.ok(railwayProject);
    }

    @GetMapping(value = "/hello")
    public Result<String> hello() {
        Result<String> result = new Result<String>();
        result.setResult("Hello World!");
        result.setSuccess(true);
        return result;
    }

    @GetMapping(value = "/statistic")
    public Result<?> statisticAction() {
        RailwayProjectStatisticVo resp = railwayProjectService.getProjectStatistic();
        return Result.OK("成功!", resp);
    }

    @GetMapping(value = "/data/statistic")
    public Result<?> dataStatisticAction(@RequestParam(name = "projectId", required = true) String projectId) {
        RailwayDataManagementStatisticVo resp = railwayProjectService.getDataManagementStatistic(projectId);
        return Result.OK("成功!", resp);
    }

    @ApiOperation("更新项目状态")
    @PostMapping(value = "/status/update")
    public Result<?> statusUpdate(@RequestBody RailwayProjectOperationLog railwayProject) {
        // log.info("railwayProject: {}, name: {}", railwayProject, railwayProject.getName());
        railwayProjectService.updateProjectStatus(railwayProject);
        return Result.OK("成功！");
    }

}
